/**
 * @namespace
 */
aws.modules.keygrabber = {
	listSubscribers: function() {
		return {
			"init": this.init
		}
	},
	/**
	 * Sets handlers to detect when user presses keys. Handlers checks if system should be notified of particular event, and, if so, sends those notifications. 
	 * Also deals with crossbrowser inconsistentcy.
	 */
	init: function() {
		/**
		 * Checks if event didn't come from form elements and no key modifiers were pressed
		 * @param {Object} e
		 */
		function targetAndModifiersOk(e) {
			return !($(e.target).is('input') || $(e.target).is('textarea') || e.ctrlKey || e.altKey);
		}
		// Webkit detects 8 only on keyup/keydown. Opera detects it on keypress and keyup/keydown, but lets prevent default action only on keypress.
		$(document).keydown(function(e) {
			if (targetAndModifiersOk(e) && e.keyCode == 8 && !$.browser.opera) {
				notify('keypress', [e.keyCode, e]);
			}
		});
		$(document).keypress(function(e) {
			if (targetAndModifiersOk(e) && e.keyCode != 8 || targetAndModifiersOk(e) && e.keyCode == 8 && $.browser.opera) {
				notify('keypress', [e.charCode || e.keyCode, e]);
			}
		});
	}
};
